CS 211 Lesson 21
Cell Arrays
Quote:
If your actions inspire others to dream more, learn more, do more and become more, you are a leader. John Quincy Adams
Lesson Objectives:
Understand the difference between normal arrays and cell arrays
Be able to create cell array variables
Be able to access values stored in cell arrays
Know when it is appropriate to use cell arrays
Lesson:
I. MATLAB Concepts
A. Introduction to non-homogeneous arrays
Up to this point in this course, all arrays have held values of a single data type. That is, an array contained all double precision values, or all character values, or all integer values, but never a mixture of these different data types.
Now we introduce two new data structures that can store values of possibly different data types in a single structure. They are:
cell arrays, which access elements within them using indexes (subscripts)
structure arrays, which access elements within them using field names
These two data structures can be nested, meaning you can have a cell array that contains structure arrays that contains other structure arrays that contain other cell arrays, etc. . You can create arbitrarily complex data relationships by nesting cell and structure arrays.
You typically organize data into cell arrays if you need to access the various element values using a numerical subscript (or index).
You typically organize data into structure arrays when the various values are best accessed using a descriptive name (called the field name).
You will better understand when to use one type over the other after you have had some experience using them.
Structure arrays are described in the next lesson, lesson 22.
B. Introduction to Cell Arrays
A cell array is a MATLAB array that can be used to store values of different data types.
Each element of a cell array is called a cell.
All cells of a cell array are 4-byte addresses (called pointers) to other data structures.
Each cell points to a distinct array, and each distinct array can possibly be of a different size and contain different types of data.
Cell arrays are often used to store arrays of strings because this allows each string to be a unique length.
For example, the statement
A = { 'Wayne', 'Sam', [5 8 2 9; 4 3 1 6] };
creates the memory shown in the diagram below. The cell array A contains three items: a 1-by-5 character array (a string), a 1-by-3 character array (a string), and a 2-by-4 double array. This example cell array will be used throughout the rest of this lesson to demonstrate cell array syntax and manipulation.
C. Cell array syntax
When specifying cells in a cell array, you have two choices and each choice does a completely different operation. Study this carefully!
Use braces { } - which is called content indexing, to retrieve the data a cell points to.
Given a cell array name with a context index, you can treat it just like a variable name for the data it points to and use any additional notation that is consist with the data it references.
For example:
A{1} is the entire 1-by-5 character array containing 'Wayne'
A{1}(3) is the single character 'y' -- that is, the 3rd value in the 1-by-5 character array 'Wayne'
A{2} is the entire 1-by-3 character array containing 'Sam'
A{2}(1) is the single character 'S' -- that is, the 1st value in the 1-by-3 character array 'Sam'
A{3} is the entire 2-by-4 double array, [5 8 2 9; 4 3 1 6]
A{3}(2,3) is the single value 1.0, which is the element on row 2 and column 3 of the array
A{1}(2,4) would generate an error message because cell 1 does not point to a 2-by-4 array
Note the use of braces { } to access a cell and the use of parentheses ( ) to access individual elements in the arrays each cell points to.
Use parentheses ( ) - which is called cell indexing, in the rare case where you need to do something to the cell pointer.
The only potential usage for cell indexing in CS211 will be to delete cells from a cell array. For example, the statement
A(2) = []; % note the use of parentheses
deletes the second cell in the A cell array. This shifts all cells with a larger index by one position. The diagram below shows what A would look like if this "delete" statement was executed.
Note the difference between the above statement and the statement
A{2} = []; % note the use of braces
which does not modify the cell array, but it deletes the data that was being pointed to by cell 2. The diagram below shows what A would look like if this "delete" statement was executed.
D. How to create cell arrays
Creation method 1: enclose a series of values within braces { }
For example:
A = { 'Wayne', 'Sam', [5 8 2 9; 4 3 1 6] };
Creation method 2: use the cell() function to pre-allocate an entire cell array and then assign each cell to its desired value.
For example:
A = cell(1,3);
A{1} = 'Wayne';
A{2} = 'Sam';
A{3} = [5 8 2 9; 4 3 1 6];
Cell arrays will grow dynamically if you use an index that is larger than the current size of the cell array.
It is more efficient to pre-allocate cell array cells with the cell() function than to make them grow dynamically. For example, to create a cell array of 40 strings, pre-allocate the cells before adding the strings:
Num_squads = 40;
Squad_names = cell(Num_squads,1);
for Squad = 1:Num_squads
Squad_names{Squad} = input(['Enter squad ' num2str(Squad) ' name: '], 's');
end
You can use the cellstr() function to convert a 2-D char array to a cell array of strings. For example:
Cell_array = cellstr(['John'; 'Jim '; 'Jo ']);
You can use the char() function to convert a cell array of strings to a 2-D char array. For example:
Char_array = char({'John', 'Jim', 'Jo'});
E. How to display/print cell arrays
The disp() function will display the contents of all cells in a cell array (or just a summary of a cell's data if the amount of data is large).
For example, disp(A) displays
'Wayne' 'Sam' [2x4 double]
The celldisp() function will display the contents of each cell of a cell array.
For example, celldisp(A) displays
A{1} =
Wayne
A{2} =
Sam
A{3} =
5 8 2 9
4 3 1 6
Typically when you write a program that requires formatted output, print the contents of cells using fprintf().
For example, the statements:
fprintf('%s had scores of %d %d %d %d\n', A{1}, A{3}(1,:));
fprintf('%s had scores of %d %d %d %d\n', A{2}, A{3}(2,:));
produces the output:
Wayne had scores of 5 8 2 9
Sam had scores of 4 3 1 6
The cellplot() function is useful for visualizing the structure of a cell array. It creates a visual plot of a cell array, as shown in the example plot of the cell array A below:
II. Good Programming Practices
In MATLAB, the way you organize data is by creating appropriate homogeneous arrays, cell arrays, and structure arrays. The design of good data structures before you start programming can greatly expedite your software development.
III. Algorithms
(None for this lesson)
Lab Work: Lab 21
References: Chapman Textbook: section 7.2